package model

import (
	"gorm.io/gorm"
	"wechat01/handler/response"
)

type Post struct {
	gorm.Model
	UserId    int    `gorm:"type:int" json:"userId"`
	Content   string `gorm:"type:varchar(100)" json:"content"`
	PostImage string `gorm:"type:varchar(255)" json:"postImage"`
	Count     int    `gorm:"type:int;default:0" json:"count"`
}

func (p *Post) AddPost(db *gorm.DB) error {
	return db.Create(&p).Error
}
func (p *Post) GetPost(db *gorm.DB, userId int) (list []response.PostList, err error) {
	err = db.Model(&p).
		Select("nick_name,content,posts.created_at,count").
		Joins("LEFT JOIN users ON users.id = posts.user_id").
		Where("users.id IN (SELECT friends.friend_id FROM friends WHERE friends.user_id = ?)", userId).
		Find(&list).Error
	return
}
